package com.example.statistics.ui.shipment

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.loadingDialog
import com.example.statistics.ui.print.BlueToothActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.PrintUtils
import com.example.statistics.utils.activityForResult
import com.example.statistics.ex.contentText
import cpcl.PrinterHelper
import kotlinx.android.synthetic.main.activity_shipment_list.*
import org.jetbrains.anko.sdk27.coroutines.onClick


/**
 * 公司商品出售列表
 */
class ShipmentListActivity : BaseActivity() {
    override val title: String
        get() = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_shipment_list)
        if (PrinterHelper.IsOpened() && !PrinterHelper.getPrintName().isNullOrBlank()) {
            val name = PrinterHelper.getPrintName()
            tv_bluetooth_status.contentText = name
            tv_bluetooth_change.text = "断开"
        } else {
            tv_bluetooth_status.contentText = "请连接打印机"
            tv_bluetooth_change.contentText = "连接"
        }

        tv_bluetooth_change.onClick {
            val text = tv_bluetooth_change.contentText
            if (text == "连接") {


                activityForResult<BlueToothActivity>() {
                    val address = it?.getStringExtra(Constants.DATA)
                    val dialog = loadingDialog("连接中...")
                    PrinterHelper.portClose()
                    PrintUtils.blueToothConnect(this@ShipmentListActivity, address.orEmpty()) { isConnected ->
                        if (isConnected) {
                            val name = PrinterHelper.getPrintName()
                            tv_bluetooth_status.contentText = name
                            tv_bluetooth_change.text = "断开"
                        } else {
                            tv_bluetooth_status.contentText = "请连接打印机"
                            tv_bluetooth_change.contentText = "连接"
                        }
                        dialog.dismiss()
                    }
                }
            } else {
                val status = PrinterHelper.portClose()
                if (status) {
                    tv_bluetooth_status.contentText = "请连接打印机"
                    tv_bluetooth_change.contentText = "连接"
                }
            }
        }

        tv_printer.onClick {
            var axis = 0
            PrinterHelper.printAreaSize("0", "200", "200", "500", "1")
            PrinterHelper.Align(PrinterHelper.CENTER)
            //表头
            axis = PrintUtils.printTitle(axis, "豫德源出库单")
            PrinterHelper.Align(PrinterHelper.LEFT)
            axis = PrintUtils.printText(axis, "郑州 -> 邯郸")
            axis = PrintUtils.printText(axis, "收货：老板")
            axis = PrintUtils.printText(axis, "发货：173 6599 1710")
            axis = PrintUtils.printText(axis, "备注：56号柱子078邯郸车")
            //表体
            axis = printColumn(axis,
                    ProductColumn("1006", "10KG", "件", "50"),
                    ProductColumn("7957", "10KL", "件", "200")
            )
            //表尾
            axis = PrintUtils.printText(axis, "地址：郑州市中原水产物流港交易大厅2楼125号")
            axis = PrintUtils.printText(axis, "主营：猪耳、熟肚、圆肚、片肚、猪手、猪脚、隔膜、前肘、后肘、胫骨等猪副产品")
            axis = PrintUtils.printText(axis, "电话：183 3999 9595")
            PrinterHelper.Form()//打印标签起定位作⽤（连续纸不可⽤）
            PrinterHelper.Print()
        }
    }

    fun printColumn(startVertical: Int, vararg column: ProductColumn): Int {
        //表体
        var axis = startVertical
        axis = PrintUtils.printLine(axis)
        axis = PrintUtils.printTextRow(axis, "商品", "规格", "单位", "件数")
        axis = PrintUtils.printLine(axis)
        column.forEach {
            axis = PrintUtils.printTextRow(axis, it.name, it.specification, it.unit, it.number)
            axis = PrintUtils.printLine(axis)
        }
        return axis
    }
}

data class ProductColumn(
        var name: String,
        var specification: String,
        var unit: String,
        var number: String,
)